package com.hsd.znsh.controller.manager;

import com.hsd.znsh.entity.Monitor;
import com.hsd.znsh.enums.MonitorTypeEnum;
import com.hsd.znsh.pojo.output.MonitorVO;
import com.hsd.znsh.pojo.output.ResultVO;
import com.hsd.znsh.pojo.query.MonitorQuery;
import com.hsd.znsh.service.MonitorService;
import com.hsd.znsh.utils.EnumUtil;
import com.hsd.znsh.utils.ResultVOUtil;
import com.hsd.znsh.utils.SecurityUtil;
import com.hsd.znsh.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-25 09:49:38
 */
@RestController
@RequestMapping("/manager/monitor")
public class ManagerMonitorController {

    @Autowired
    private MonitorService monitorService;

    @GetMapping("/getMonitorTypeList")
    public ResultVO getMonitorTypeList(){
        List<Map<String,Object>> list=EnumUtil.getEnumInfo(MonitorTypeEnum.class,"monitorType");
        return ResultVOUtil.success(list);
    }

    @PostMapping("/getMonitorList")
    public ResultVO getMonitorList(@PageableDefault(sort = "updateDateTime" ,direction = Sort.Direction.DESC) Pageable pageable, MonitorQuery monitorQuery){
        List<MonitorVO> list;
        if(SecurityUtil.hasRole("ROLE_ROOT")){
            list=monitorService.getMonitorList(pageable,monitorQuery,null);
        }else{
            list=monitorService.getMonitorList(pageable,monitorQuery,SecurityUtil.getUserId());
        }
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getMonitorList")
    public ResultVO getMonitorList(Integer size, MonitorQuery monitorQuery){
        long totalNum;
        if(SecurityUtil.hasRole("ROLE_ROOT")){
            totalNum=monitorService.getMonitorListTotalPages(monitorQuery,null);
        }else{
            totalNum=monitorService.getMonitorListTotalPages(monitorQuery,SecurityUtil.getUserId());
        }
        return ResultVOUtil.success((long)Math.ceil(totalNum/(double)size));
    }

    @GetMapping("/getMonitorInfo")
    public ResultVO getMonitorInfo(Integer id){
        MonitorVO monitorVO= monitorService.getMonitorInfo(id);
        return ResultVOUtil.success(monitorVO);
    }

    @PostMapping("/editMonitor")
    public ResultVO editMonitor(@Valid Monitor monitor, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        if(monitor.getId()==null){
            monitorService.addMonitor(monitor);
            return ResultVOUtil.success("添加成功");
        }else{
            monitorService.updateMonitor(monitor);
            return ResultVOUtil.success("修改成功");
        }
    }

    @PostMapping("/deleteMonitor")
    public ResultVO deleteMonitor(Integer id) {
        monitorService.deleteMonitor(id);
        return ResultVOUtil.success("删除成功");
    }
}
